<?php

namespace Admin\Controller;

use Utuo\BackgroundController;

class ModelController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle("模型管理");
		$this->setActionTitle(array(
			'index' => '模型列表',
			'add' => '模型添加',
			'modify' => '修改模型',
		));
		$this->setActionNav(array(
			'index',
			'add',
		));
	}
	
	// 模型列表
	public function indexAction(){
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$modelLists = M('Model')->limit($pagesize)->page($page)->select();
		$modelCount = M('Model')->count();
		$this->assign('modelLists', $modelLists);
		$this->showPage($modelCount, $pagesize); 
		$this->display();
	}
	
	// 模型添加
	public function addAction(){
		$this->display();
	}
	
	
	// 提交添加
	public function addPostAction() {
		$m = D('Model');
		if ($m->create()) {
			if ($m->add()) {
				$this->ajaxSuccess('添加模型成功');
			} else {
				$this->ajaxError($m->getError());
			}
		} else {
			$this->ajaxError($m->getError());
		}
	}
	
	// 修改模型
	public function modifyAction() {
		$model = $this->getOneById("Model");
		$this->assign('model', $model);
		$this->display();
	}
	
	// 提交修改模型
	public function modifyPostAction() {
		$d = D('Model');
		if ($d->create()) {
			if (false!==$d->save()) {
				$this->ajaxSuccess('修改模型成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
}